package com.sugon.cloudgateway.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * gateway 配置类
 *
 * @author JH
 * @version 1.0
 * @since 2023/2/21 16:29
 */
@Configuration
public class GatewayConfig {

    /*
    配置了一个id为path_route的路由规则，当访问http://localhost:9527/guonei时
    会自动转发到http://news.baidu.com/guonei
     */
    // RouteLocator 路由定位器
    // RouteLocatorBuilder 路由构建器
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        routes.route("path_route", r ->
                r.path("/guonei").uri("https://news.baidu.com/guonei")
        ).build();
        return routes.build();
    }

}
